
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[EuroOdds_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[EuroOdds_Insert]

IF OBJECT_ID(N'[dbo].[EuroOdds_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[EuroOdds_Update]

IF OBJECT_ID(N'[dbo].[EuroOdds_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[EuroOdds_Delete]

IF OBJECT_ID(N'[dbo].[EuroOdds_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[EuroOdds_Select]

--endregion

GO


--region [dbo].[EuroOdds_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[EuroOdds_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdEuroOdd int = null,
	@IdMatch int = null,
	@Home float = null,
	@Away float = null,
	@Draw float = null,
	@OddsType nvarchar(50) = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[EuroOdds]
					WHERE
					(
						(@IdEuroOdd is null OR [EuroOdds].[IdEuroOdd] = @IdEuroOdd)
						AND (@IdMatch is null OR [EuroOdds].[IdMatch] = @IdMatch)
						AND (@Home is null OR [EuroOdds].[Home] = @Home)
						AND (@Away is null OR [EuroOdds].[Away] = @Away)
						AND (@Draw is null OR [EuroOdds].[Draw] = @Draw)
						AND (@OddsType is null OR [EuroOdds].[OddsType] like @OddsType)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[EuroOdds].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdEuroOdd' and @SortType = 1 THEN [EuroOdds].[IdEuroOdd] END ASC,
						CASE WHEN @SortBy = 'IdEuroOdd' and @SortType = 0 THEN [EuroOdds].[IdEuroOdd] END DESC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 1 THEN [EuroOdds].[IdMatch] END ASC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 0 THEN [EuroOdds].[IdMatch] END DESC,
						CASE WHEN @SortBy = 'Home' and @SortType = 1 THEN [EuroOdds].[Home] END ASC,
						CASE WHEN @SortBy = 'Home' and @SortType = 0 THEN [EuroOdds].[Home] END DESC,
						CASE WHEN @SortBy = 'Away' and @SortType = 1 THEN [EuroOdds].[Away] END ASC,
						CASE WHEN @SortBy = 'Away' and @SortType = 0 THEN [EuroOdds].[Away] END DESC,
						CASE WHEN @SortBy = 'Draw' and @SortType = 1 THEN [EuroOdds].[Draw] END ASC,
						CASE WHEN @SortBy = 'Draw' and @SortType = 0 THEN [EuroOdds].[Draw] END DESC,
						CASE WHEN @SortBy = 'OddsType' and @SortType = 1 THEN [EuroOdds].[OddsType] END ASC,
						CASE WHEN @SortBy = 'OddsType' and @SortType = 0 THEN [EuroOdds].[OddsType] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[EuroOdds]
			Where 
			(
				(@IdEuroOdd is null OR [EuroOdds].[IdEuroOdd] = @IdEuroOdd)
				AND (@IdMatch is null OR [EuroOdds].[IdMatch] = @IdMatch)
				AND (@Home is null OR [EuroOdds].[Home] = @Home)
				AND (@Away is null OR [EuroOdds].[Away] = @Away)
				AND (@Draw is null OR [EuroOdds].[Draw] = @Draw)
				AND (@OddsType is null OR [EuroOdds].[OddsType] like @OddsType)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[EuroOdds_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[EuroOdds_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[EuroOdds_Insert]
	@IdEuroOdd int OUTPUT,
	@IdMatch int,
	@Home float,
	@Away float,
	@Draw float,
	@OddsType nvarchar(50)

AS


INSERT INTO [dbo].[EuroOdds] 
(
	[IdMatch],
	[Home],
	[Away],
	[Draw],
	[OddsType]
)
VALUES 
(
	@IdMatch,
	@Home,
	@Away,
	@Draw,
	@OddsType
)

SET @IdEuroOdd = SCOPE_IDENTITY()

--end [dbo].[EuroOdds_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[EuroOdds_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[EuroOdds_Update]
	@IdEuroOdd int,
	@IdMatch int,
	@Home float,
	@Away float,
	@Draw float,
	@OddsType nvarchar(50)
AS


UPDATE [dbo].[EuroOdds] SET
	[IdMatch] = @IdMatch,
	[Home] = @Home,
	[Away] = @Away,
	[Draw] = @Draw,
	[OddsType] = @OddsType
WHERE
	[IdEuroOdd] = @IdEuroOdd

--end [dbo].[EuroOdds_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[EuroOdds_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[EuroOdds_Delete]
	@IdEuroOdd int
AS


DELETE FROM [dbo].[EuroOdds]
WHERE
(
	[IdEuroOdd] = @IdEuroOdd
)

--end [dbo].[EuroOdds_Delete]
--endregion

GO
--=========================================================================================--

